package com.zxs.order.controller;

import com.zxs.order.parm.OrderPramId;
import com.zxs.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import parm.CartListParm;
import parm.OrderParam;
import utils.R;

/**
 * @author zxs
 * *8 22:30
 */
@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 订单数据保存
     *
     * @param orderParam
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody OrderParam orderParam) {

        return orderService.save(orderParam);
    }

    /**
     * 查询订单，有多个，根据用户id查
     * @param cartListParm
     * @return
     */
    @PostMapping("list")
    public R list(@RequestBody @Validated CartListParm cartListParm, BindingResult result) {
        if (result.hasErrors()){
            return R.fail("参数异常，订单无法查询！");
        }
        return orderService.list(cartListParm.getUserId());
    }
    /**
     * 根据数据库id删除订单
     */
    @PostMapping("remove")
    public R removeOrder(@RequestBody @Validated OrderPramId orderPramId, BindingResult result) {
        if (result.hasErrors()){
            return R.fail("参数异常，订单无法查询！");
        }
        System.out.println("orderPramId.getOrderId() = " + orderPramId.getOrderId());

        return orderService.removeByOrderId(orderPramId.getOrderId());

    }


}
